package com.lg.demowb.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.lg.demowb.config.AppCode;
import com.weibo.net.AccessToken;
import com.weibo.net.DialogError;
import com.weibo.net.Utility;
import com.weibo.net.Weibo;
import com.weibo.net.WeiboDialogListener;
import com.weibo.net.WeiboException;

public class MainActivity extends Activity {
	private Button mLogin;
	private TextView mToken;

	private static final String URL_ACTIVITY_CALLBACK = "weiboandroidsdk://TimeLineActivity";
	private static final String FROM = "xweibo";

	private static final String CONSUMER_KEY = AppCode.CONSUMER_KEY;
	private static final String CONSUMER_SECRET = AppCode.CONSUMER_SECRET;

	private String username = "";
	private String password = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mToken = (TextView) this.findViewById(R.id.tvToken);
		mLogin = (Button) this.findViewById(R.id.btnLogin);
		mLogin.setText("oauth!");
		Weibo weibo = Weibo.getInstance();

		weibo.setupConsumerConfig(CONSUMER_KEY, CONSUMER_SECRET);

		weibo.setRedirectUrl("http://weibo.com");

		weibo.authorize(MainActivity.this, new AuthDialogListener());
	}

	public void onResume() {
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		Utility.clearCookies(this);
		super.onDestroy();
	}

	class AuthDialogListener implements WeiboDialogListener {

		@Override
		public void onComplete(Bundle values) {
			String token = values.getString("access_token");
			String expires_in = values.getString("expires_in");
			mToken.setText("access_token : " + token + "  expires_in: "
					+ expires_in);
			AccessToken accessToken = new AccessToken(token, CONSUMER_SECRET);
			accessToken.setExpiresIn(expires_in);
			Weibo.getInstance().setAccessToken(accessToken);
			Intent intent = new Intent();
			intent.setClass(MainActivity.this, TestActivity.class);
			startActivity(intent);
		}

		@Override
		public void onError(DialogError e) {
			Toast.makeText(getApplicationContext(),
					"Auth error : " + e.getMessage(), Toast.LENGTH_LONG).show();
		}

		@Override
		public void onCancel() {
			Toast.makeText(getApplicationContext(), "Auth cancel",
					Toast.LENGTH_LONG).show();
		}

		@Override
		public void onWeiboException(WeiboException e) {
			Toast.makeText(getApplicationContext(),
					"Auth exception : " + e.getMessage(), Toast.LENGTH_LONG)
					.show();
		}

	}

}